import Vue from 'vue'
import Vuex from 'vuex'
import Config from "@/common/config.js"
import { getMedias,wxLogin,updateUserInfo } from '../api/app/api'

Vue.use(Vuex)

const store = new Vuex.Store({
	state:{
		// blackpink信息
		userInfo:{},
		// 用户信息
		user:{},
		showMask:false,
		icons:{
			optionIcon:Config.static_url + "icons/option.png",
			headPortrait: Config.static_url + "HeadPortrait.jpg",
			ai: Config.static_url + "icons/@.png",
			sendIcon: Config.static_url + "icons/send.png",
      		labelIcon: Config.static_url + "icons/label.png",
			returnIcon: Config.static_url + "icons/return.png",
			play: Config.static_url + "icons/play.png",
			videoIcon:Config.base_Url + "icons/video.png",
			sync:Config.base_Url + "icons/sync.png",
			vip: Config.base_Url + "icons/VIP.png",
			noVip: Config.base_Url + "icons/NOVIP.png",
		},
		fanShow:true,
		videoContext:"",
		systeminfo:"",
		pixelRatio:2,
		isLogin:false,
		userEdge:[],
		roseEdge:[],
		aiImgs:[],
		config:{}
	},
	mutations:{
		setUserInfo(state,datas){
			this.userInfo[datas["key"]] = datas["value"];
		},
		setPixelRatio(state,data){
			state.pixelRatio = data;
		},
		setUserInfos(state,datas){
			state.userInfo = datas;
		},
		setOpenId(state,data){
			state.user.openId = data.openId;
		},
		setUser(state,data){
			state.user = Object.assign({},state.user,data);
			console.log(state.user)
			if(!uni.getStorageSync('userinfo')){
				uni.setStorageSync('userinfo',JSON.stringify(data));
			}
			state.isLogin = true;
		},
		addAiImg(state,data){
			state.aiImgs.push(data)
		},
		delAiImg(state){
			state.aiImgs.splice(0,1);
		},
		setMask(state,data){
			state.showMask = data;
		},
		setShow(state,data){
			state.fanShow = data;
		},
		setVideoContext(state,data){
			state.videoContext = data;
		},
		setPlatform(state,data){
			console.log('data是:',data)
			state.systeminfo = data;
		},
		setLogin(state,login){
			state.isLogin = login;
		},
		setUserEdge(state,data){
			if(Array.isArray(data)){
				state.userEdge.push(...data)
			}else{
				state.userEdge.push(data)
			}
		},
		setRoseEdge(state,data){
			state.roseEdge.push(...data)
		},
		setConfig(state,data){
			state.config = Object.assign(state.config, data)
		},
	},
	actions:{
		async changeUserInfo({ commit, state },data){
			let userInfo = state.user = Object.assign({},state.user,data)
			updateUserInfo(userInfo).then(res=>{
				console.log(res)
			}).catch(err=>{
				console.log('err',err)
			})
		},
		login({commit,state},data){
			wx.login({
				success:res=>{
					wxLogin()
				}
			})
		}
	},
	getters:{
		getIcons(state){
			return state.icons;
		},
		getConfig(state){
			return state.config
		},
		getRoseEdge(state){
			return state.roseEdge
		},
		userName(state){
			return state.userInfo.username ?? "roses_are_rosie";
		},
		getUserEdge(state){
			return state.userEdge;
		},
		getAiImgs(state){
			return state.aiImgs;
		},
		getUser(state){
			return state.user;
		},
		getLogin(state){
			return state.isLogin;
		},
		showMasks(state){
			return state.showMask;
		},
		optionIcon(state){
			return state.icons.optionIcon;
		},
		getShow(state){
			return state.fanShow;
		},
		getVideoContext(state){
			return state.videoContext;
		},
		getsysteminfo(state){
			return state.systeminfo;
		},
		getUserInfo(state){
			return state.userInfo;
		},
		getPixelRatio(state){
			return state.pixelRatio / 2;
		},
		getVipExpired(state){
			const user = state.user
			return user.isVip == 1 && user.isVipExpire != 0 && (new Date(user.isVipExpire) - new Date()) > 0
		},
	}
})

export default store;